<?php
/**
 * 格式化打印数组
 * @param  [Array] $arr [需要打印的数组]
 * @return [void]
 */
function p ($arr) {
	echo '<pre>';
	print_r($arr);
	echo '</pre>';
}

/**
 * 递归组合无限级分类
 * @param  [Arrray]  $arr [需要处理的数组]
 * @param  integer $pid [父级ID]
 * @return [Array]
 */
function recursion ($arr, $pid=0, $type=1, $fid=0, $level=0) {
	switch($type) {
		case 1 :	//组合多维数组
			 $array = array();
			foreach ($arr as $v) {
				if ($v['pid'] == $pid) {
					$array[$v['id']] = $v;
					$array[$v['id']]['child'] = recursion($arr, $v['id']);
				}
			}
			return $array;

		case 2 :	//组合一维数组
			static $array = array();
			static $i = -1;
			$i++;
			foreach ($arr as $v) {
				if ($v['pid'] == $pid) {
					$array[$i] = $v;
					$array[$i]['lv'] = $level;
					$array[$i]['html'] = str_repeat('--', $level);
					recursion($arr, $v['id'], $type, $sid, $level + 1);
				}
			}
			return $array;

		case 3 :	//传递分类ID返回所有父级
		 	static $array = array();
		 	foreach ($arr as $v) {
		 		if ($v['id'] == $fid) {
		 			$array[$v['id']] = $v;
		 			recursion($arr, $pid, $type, $v['pid']);
		 		}
		 	}
		 	return array_reverse($array);
			
		case 4 : //传递一个ID 返回所有子栏目ID
			static $array = array();
			//$array[] = $fid;
			foreach ($arr as $v) {
				if ($v['pid'] == $fid) {
					$array[] = $v['id'];
					recursion($arr, $pid, $type, $v['id']);
				}
			}
			return $array;
	}
}
/**
 * 重组URL
 * @param  [type] $filter [filter参数数组]
 * @param  [type] $key    [当前索引值]
 * @param  [type] $value  [属性ID]
 * @return [type]         [description]
 */
 function filter_url($filter,$key,$value){
 	$filter[$key]=$value;
 	$get=$_GET['_URL_'];
 	$key=array_search('filter',$get);
 	if($key){
 		unset($get[$key]);
 		unset($get[$key+1]);
 	}
 	$url=__APP__;
 	foreach($get as $v){
 		$url.='/'.$v;
 	}
 	$url.='/filter/'.implode('_',$filter);
 	return $url;
 }	
 //传递分类ID返回所有父级
function parentid ($arr,$fid){
	static $array = array();
 	foreach ($arr as $v) {
 		if ($v['id'] == $fid) {
 			$array[$v['id']] = $v;
 			parentid($arr,$v['pid']);
 		}
 	}
 	return array_reverse($array);
}
//传递分类ID返回顶级父类
function topid($arr,$id){
	static $top=array();
	foreach($arr as $v){
		if($v['id']==$id){
			if($v['pid']==0){
				$top=$v;
			}else{
				topid($arr,$v['pid']);
			}
		}
	}
	return $top;
}
//传递一个分类id返回所有的子类
function childid($arr,$fid){
		 $array = array();
		//$array[] = $fid;
		foreach ($arr as $v) {
			if ($v['pid'] == $fid) {
				$array[$v['id']] = $v;
				//childid($arr,$v['id']);
			    $array[$v['id']]['child'] = childid($arr,$v['id']);
			}
		}
		return $array;
}
?>
